<?

include_once ("Server.php");
include_once ("config.php");
include_once ("mailto.php");

class SelectionRepo extends BaseRepo {
	
	protected function getEntityName(){
		return "Selection";
	}
	
	public function getRecentSelections($num, $draftID)
	{
		$con = connect();
		$q = "ORDER BY pick DESC ";
		if ($num > 0)
		{
			$q = $q."LIMIT ".$num;
		}
		$selectionRows = $this->get($q);
		return $selectionRows;
	}

	function addSelection($playerID, $team, $draft)
	{
		if ($team->isInDraft($draft->id) && $draft->isOnClock($team->draftOrder))
		{
			$con = connect();
			$query = "INSERT INTO selection (team_id, player_id, round, pick, draft_id) ";
			$query = $query . "VALUES ('" . $team->id . "','" . $playerID . "','" . $draft->currentRound . "','" . $draft->currentPick . "','" . $draft->id . "')";
			query($query, $con);
			disconnect($con);
			updateTextFile();
			$inc = incrementDraft($draft);
			$draft = getDraft($draft->id);
			$teamOnClock = getPickingTeam($draft);
			$user = getUserFromTeam($teamOnClock->id);
			contactUser($user);
			
			$selections = getRecentSelections(1, $draft->id);
			$optInUsers = getAllUsersToSeeUpdates($draft->id);
			mailToAllUsers($optInUsers, $selections[0]);
			
			return $inc;
		}
		return false;
	}

	function updateTextFile()
	{
		$fname = "mod.html"; 
		$out = time();
		$f = fopen($fname, "w");
		fwrite($f, $out); 
		fclose($f); 
	}
}

?>